package defaultproperty;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import pool.tablename.Pool;
import property.Property;

import utility.ConvertToLower;
import utility.Input;
import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;
import file.SearchRecord;

public class Transaction_details implements Defaultproperty 
{	
	Pool masterobject;
	DBManager database;
	GetIdName gid;
	Hashtable<Object, Object> TransactionDetailMap;
	Hashtable<Object, Object> table =new Hashtable<Object, Object>();
	String TDID="tdid";
	String VALUEOBJECT="valueobject"; 
	String TABLEANME="Transaction_details";
	String TYPE="type";
	
	public void getResult(String value) 
	{
		try
		{
			RecordSearch sr =new RecordSearch(database);
			Hashtable<Object, Object > condition=new Hashtable<Object, Object>();
			condition.put("mid", value);	
			condition.put("ref", TABLEANME);
			condition.put("td", "null");
			sr.setConditionMap(condition);
			String result[] = sr.getArrayFromResultSet();
			for(int i=0;i<result.length;i++)
			{
				String splitresult[] = result[i].split("\t");
				Property propertyobje = new Property(database); 
				propertyobje.setMid(splitresult[Column.pv_index-1]);
				propertyobje.setPid(TYPE);
				String type = propertyobje.getValue();
				String property = gid.getItem(splitresult[Column.pid_index-1]);
				if(!property.equalsIgnoreCase("type"))
				{
					if(!type.equalsIgnoreCase(VALUEOBJECT))
					{
						String pv = splitresult[Column.pv_index-1];
						if(pv.equals("0"))
							pv = splitresult[Column.vt_index-1];
						else
							pv = gid.getItem(pv);
						if(!TransactionDetailMap.containsKey(property))
							TransactionDetailMap.put(property, pv);
					}

					{
					condition.clear();
					condition.put("mid", splitresult[Column.pv_index-1]);
					condition.put("td","null");
					sr.setConditionMap(condition);
					String line[]=sr.getArrayFromResultSet();
					
					for(int j=0;j<line.length;j++)
					{
						String splitline[] = line[j].split("\t");
						if(!splitline[Column.pid_index-1].equals(TYPE))      
						 if(!TransactionDetailMap.containsKey(property))
						{
							int k=checkCondition(splitline[Column.pv_index-1]);
							if(k==1)
							{
								GetDefaultvalue getDefaultvalue = new GetDefaultvalue(database,masterobject);
								String defaultvalue=getDefaultvalue.getdefaultvalue(splitline[Column.pid_index-1], splitline[Column.pv_index-1]);
								if(!defaultvalue.equalsIgnoreCase("error"))
									if(!TransactionDetailMap.containsKey(property))
										TransactionDetailMap.put(property, defaultvalue);
							}
							else	if(k==2)
							{
								if(!TransactionDetailMap.containsKey(property))
									TransactionDetailMap.put(property, splitline[Column.pv_index-1]);
							}	
						}
					}
					}
				}
			}
		} catch (Exception e) 
		{	
			e.printStackTrace();
		}
		masterobject.setHashtable(TransactionDetailMap);
	}
	
	public int checkCondition(String conditionobject)
	{	
		try 
		{
			RecordSearch sr = new RecordSearch(database);
			Hashtable<Object, Object> condition = new Hashtable<Object, Object>();
			condition.put("mid", conditionobject);
			condition.put("td", "null");
			sr.setConditionMap(condition);
			String line[] = sr.getArrayFromResultSet();
			table.clear();
			for(int i=0;i<line.length;i++)
			{
				String splitline[]=line[i].split("\t");
				String value = splitline[Column.pv_index-1];
				if(value.equals("0"))
					value = splitline[Column.vt_index-1];
				else
					value = gid.getItem(value);
				table.put(gid.getItem(splitline[Column.pid_index-1]), value);
			}
			table = ConvertToLower.convertHashKey(table);
			table = ConvertToLower.convertHashKey(table);
            if(table.containsKey("class"))
                            return 1;
            
			String conditionvalue="";
			String conditionfield="";
			try {
				conditionvalue = table.get("conditionvalue").toString();
				conditionfield = table.get("conditionfield").toString();
			} catch (Exception e) 
			{
				e.printStackTrace();
				return 1;
			}		
			if(TransactionDetailMap.containsKey(conditionfield))
			{
				if(TransactionDetailMap.get(conditionfield).equals(conditionvalue))
					return 1;
				else
					return 0;
			}
			return 0;
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			return 0;
		}
	}

	public void setObject(Object object, DBManager database) 
	{
		this.database = database; 
		this.masterobject =(Pool) object; 
		TransactionDetailMap = masterobject.getHashtabel();
		gid =new GetIdName(database);
		TYPE  = gid.getId(TYPE);
		TDID = gid.getId(TDID);
		TABLEANME = gid.getId(TABLEANME);
	}
	
	public Hashtable<Object, Object> getAllproperties() 
	{
		Hashtable<Object, Object> propertyMap = new Hashtable<Object, Object>();
		Enumeration<Object> enumoftransactiomap = TransactionDetailMap.keys();
		while(enumoftransactiomap.hasMoreElements())
		{
			String key = enumoftransactiomap.nextElement().toString();
			String value = TransactionDetailMap.get(key).toString();
			if(propertyMap.containsKey(key))
			{
				int count = Integer.parseInt(propertyMap.get(key).toString());
				propertyMap.put(key,Integer.toString(count++ ));
			}
			else
				propertyMap.put(key,"1");
		}
		if(TransactionDetailMap.containsKey("tid"))
		{
			String id = TransactionDetailMap.get("tid").toString();
			String TDPLPATH =Input.TDPRODETAILSREPORT + TDID + "//" + id + Input.FILRFORMAT;
			SearchRecord sr =new SearchRecord(TDPLPATH);
			Hashtable<Object, Object> QueryMap = new Hashtable<Object, Object>();
			QueryMap.put("tdid", id);			
			String line[] = sr.getRecordSet(QueryMap);
			for(int i=0;i<line.length;i++)
			{
				String splitline[] = line[i].split("\t");
				if(propertyMap.containsKey(splitline[Column.pid_index-1]))
				{
					int count = Integer.parseInt(propertyMap.get(splitline[Column.pid_index-1]).toString());
					propertyMap.put(gid.getItem(splitline[Column.pid_index-1]),Integer.toString(++count));
				}
				else
					propertyMap.put(gid.getItem(splitline[Column.pid_index-1]),"1");
			}
		}
		return propertyMap;
	}

	
	
}
